package com.sys.server.modules.controller;

import com.sys.server.common.annotation.Authentication;
import com.sys.server.common.annotation.Log;
import com.sys.server.common.model.R;
import com.sys.server.modules.controller.dto.PathDto;
import com.sys.server.modules.controller.dto.StandardDto;
import com.sys.server.modules.service.FileService;
import com.sys.server.modules.service.NormasStandardService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Authentication
@RestController
@Api(tags = "行业标准模块")
public class NormasStandardController {

    private static final Logger LOG = LoggerFactory.getLogger(NormasStandardController.class);

    @Autowired
    private NormasStandardService standardService;

    @Autowired
    private FileService fileService;

    @RequestMapping(value = "/addStandard", method = RequestMethod.POST)
    @ApiOperation(value = "行业标准模块--新增行业标准")
//    @Log(module = "行业标准", action = "新增行业标准", type = "新增")
    public R createStandard(@ApiParam(value = "行业标准信息") StandardDto standardDto) {
        return standardService.createStandard(standardDto);
    }

    @RequestMapping(value = "/getStandardList", method = RequestMethod.GET)
    @ApiOperation(value = "行业标准模块--获取行业标准列表")
    public R getStandardList(@ApiParam(value = "当前页") @RequestParam Long page,
                             @ApiParam(value = "每页记录数") @RequestParam Long limit) {
        return standardService.getStandardList(page, limit);
    }

    @RequestMapping(value = "/searchStandardDetail", method = RequestMethod.GET)
    @ApiOperation(value = "行业标准模块--获取行业标准详细信息")
    public R getMaintext(@ApiParam(value = "行业标准号") @RequestParam String number) {
        return standardService.getDetailInfo(number);
    }

    @RequestMapping(value = "/downloadStandardAttach", method = RequestMethod.POST)
    @ApiOperation(value = "行业标准模块--下载行业标准附件")
    public void downloadStandardAttach(HttpServletResponse response, @RequestBody PathDto pathDto) throws IOException {
        String path = standardService.downloadStandardAttach(pathDto.getPath());
        fileService.fileDownload(response, path);
    }

    @RequestMapping(value = "/updateStandardInfo", method = RequestMethod.POST)
    @ApiOperation(value = "行业标准模块--修改行业标准（不上传附件）")
    @Log(module = "行业标准", action = "修改行业标准", type = "修改")
    public R updateStandardInfo(@RequestBody StandardDto standardDto) {
        return standardService.updateStandardInfo(standardDto);
    }

    @RequestMapping(value = "/deleteStandard", method = RequestMethod.POST)
    @ApiOperation(value = "行业标准模块--删除行业标准")
    @Log(module = "行业标准", action = "删除行业标准", type = "删除")
    public R deleteStandard(@RequestBody StandardDto standardDto) {
        return standardService.deleteStandard(standardDto);
    }

}
